<template>
  <div class="basic_info">
    <div class="reset">
      <div class="resetInfo">
        <div class="title">价格优先级设置</div>
      </div>
      <div class="btn">
        <el-button size="mini" style="border-radius: 0.093756rem" :class="index === 1 ? 'isActive' : ''"
          @click="market(1)">销售价格</el-button>
        <el-button v-if="!isBaseVersion" size="mini" style="border-radius: 0.093756rem" :class="index === 2 ? 'isActive' : ''"
          @click="purchase(2)">采购价格</el-button>
        <el-button size="mini" style="border-radius: 0.093756rem" :class="index === 3 ? 'isActive' : ''"
          @click="discount(3)">销售折扣率</el-button>
        <el-button v-if="!isBaseVersion" size="mini" style="border-radius: 0.093756rem" :class="index === 4 ? 'isActive' : ''"
          @click="rateOfAdoption(4)">采购折扣率</el-button>
      </div>
      <div class="gather">
        <PublicComponent :tableData="tableData"></PublicComponent>
        <div class="isSave">
          <el-button size="mini" @click="handleSave">保存</el-button>
          <el-button size="mini" @click="handleClose">关闭</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import PublicComponent from '@/views/setting/pricePriority/PublicComponent.vue'
import { getAction } from '@/api/manage'
import { USER_INFO, ACCOUNT_BOOK } from '@/store/mutation-types'
import Vue from 'vue'
import { postAction, putAction } from '@/api/manage'
import { setToken } from '@/utils/auth'
import { handleIsBaseVersion } from '@/utils'

export default {
  name: 'pricePriority',
  data() {
    return {
      isSave: false,
      // 1-销售价格默认值
      tableData1: [
        {
          sysOrgCode: null,
          configType: '1',
          idx: 1,
          priorityCode: '1',
          priorityName: '客户最近价',
          explains: '客户最近的一次销售价',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '1',
          idx: 2,
          priorityCode: '2',
          priorityName: '客户预设价',
          explains: '客户基本信息中的预设售价',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '1',
          idx: 3,
          priorityCode: '3',
          priorityName: '商品最近价',
          explains: '商品最近一次销售价',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '1',
          idx: 4,
          priorityCode: '4',
          priorityName: '商品零售价',
          explains: '商品基本信息中的零售价',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '1',
          idx: 5,
          priorityCode: '5',
          priorityName: '商品预设售价一',
          explains: '商品基本信息中的预设售价一',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '1',
          idx: 6,
          priorityCode: '6',
          priorityName: '商品预设售价二',
          explains: '商品基本信息中的预设售价二',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '1',
          idx: 7,
          priorityCode: '7',
          priorityName: '商品成本价',
          explains: '商品成本均价',
          status: '0',
          val: null,
        },
      ],
      // 2-采购价格默认值
      tableData2: [
        {
          sysOrgCode: null,
          configType: '2',
          idx: 1,
          priorityCode: '1',
          priorityName: '商品最近价',
          explains: '商品商品最近一次采购价',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '2',
          idx: 2,
          priorityCode: '2',
          priorityName: '供应商最近价',
          explains: '供应商最近一次采购价',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '2',
          idx: 3,
          priorityCode: '3',
          priorityName: '商品预设进价一',
          explains: '商品基本信息中的预设进价一',
          status: '0',
          val: null,
        },
      ],
      // 3-销售折扣率默认值
      tableData3: [
        {
          sysOrgCode: null,
          configType: '3',
          idx: 1,
          priorityCode: '1',
          priorityName: '客户预设折扣',
          explains: '客户基本信息中的预设折扣',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '3',
          idx: 2,
          priorityCode: '2',
          priorityName: '商品预设折扣一',
          explains: '商品基本信息中的预设折扣一',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '3',
          idx: 3,
          priorityCode: '3',
          priorityName: '商品预设折扣二',
          explains: '商品基本信息中的预设折扣二',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '3',
          idx: 6,
          priorityCode: '4',
          priorityName: '客户最近折扣',
          explains: '客户最近一次折扣',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '3',
          idx: 7,
          priorityCode: '5',
          priorityName: '商品最近折扣',
          explains: '商品最近一次折扣',
          status: '0',
          val: null,
        },
      ],
      // 4-采购折扣率默认值
      tableData4: [
        {
          sysOrgCode: null,
          configType: '4',
          idx: 1,
          priorityCode: '1',
          priorityName: '供应商最近折扣',
          explains: '供应商的最近一次折扣',
          status: '0',
          val: null,
        },
        {
          sysOrgCode: null,
          configType: '4',
          idx: 2,
          priorityCode: '2',
          priorityName: '商品最近折扣',
          explains: '商品的最近一次折扣',
          status: '0',
          val: null,
        },
      ],
      tableData: [],
      index: 1,
      accountBookId: '',
    }
  },
  computed: {
    isBaseVersion() {
      return handleIsBaseVersion()
    }
  },
  created() {
    let accountBook = Vue.ls.get(ACCOUNT_BOOK)
    this.accountBookId = accountBook && accountBook.id
    // 如果是必用版，只能查看销售价格
    if (this.isBaseVersion) {
      this.index = 1
    }
    this.getPriorityConfig(1)
  },
  methods: {
    // 获取数据
    getPriorityConfig(type) {
      // 如果是必用版，不允许查看采购相关配置
      if (this.isBaseVersion && (type === 2 || type === 4)) {
        return
      }
      
      getAction('/xghPriorityConfig/xghPriorityConfig/queryById', {
        configType: type,
        xghAccountBookId: this.accountBookId,
      }).then((res) => {
        if (res.code == 200) {
          let list = res.result.xghPriorityConfigList || []
          if (list.length > 0) {
            // 如果是必用版且是销售价格配置，需要过滤掉成本价选项
            if (this.isBaseVersion && type === 1) {
              list = list.filter(item => item.priorityName !== '商品成本价')
            }
            this.tableData = []
            for (let index = 0; index < 7; index++) {
              if (list[index] && list[index].priorityName) {
                this.tableData.push(list[index])
              } else {
                this.tableData.push({})
              }
            }
            this.isSave = false
          } else {
            this.handleData(type)
          }
        } else {
          this.handleData(type)
        }
      })
    },
    // 处理数据
    handleData(type) {
      this.isSave = true
      if (type == 1) {
        // 销售价格，如果是必用版则过滤掉成本价选项
        let data = JSON.parse(JSON.stringify(this.tableData1))
        if (this.isBaseVersion) {
          data = data.filter(item => item.priorityName !== '商品成本价')
        }
        this.tableData = data
      } else if (type == 2) {
        this.tableData = JSON.parse(JSON.stringify(this.tableData2))
        this.tableData.push({})
        this.tableData.push({})
        this.tableData.push({})
        this.tableData.push({})
      } else if (type == 3) {
        this.tableData = JSON.parse(JSON.stringify(this.tableData3))
        this.tableData.push({})
        this.tableData.push({})
      } else if (type == 4) {
        this.tableData = JSON.parse(JSON.stringify(this.tableData4))
        this.tableData.push({})
        this.tableData.push({})
        this.tableData.push({})
        this.tableData.push({})
        this.tableData.push({})
      }
    },
    purchase(index) {
      if (this.isBaseVersion) return
      this.index = 2
      this.getPriorityConfig(index)
    },
    market(index) {
      this.index = 1
      this.getPriorityConfig(index)
    },
    discount(index) {
      this.index = 3
      this.getPriorityConfig(index)
    },
    rateOfAdoption(index) {
      if (this.isBaseVersion) return
      this.index = 4
      this.getPriorityConfig(index)
    },
    // 保存
    handleSave() {
      let list = []
      this.tableData.forEach(el => {
        if (el.priorityName && el.priorityName.length != ' ') {
          list.push(el)
        }
      })
      let params = {
        xghAccountBookId: this.accountBookId,
        xghPriorityConfigList: list,
      }
      if (this.isSave) {
        // 保存
        postAction('/xghPriorityConfig/xghPriorityConfig/add', params).then((res) => {
          if (res.code == 200) {
            this.$elMessage.success(res.message)
          } else {
            this.$elMessage.error(res.message)
          }
        })
      } else {
        // 编辑
        putAction('/xghPriorityConfig/xghPriorityConfig/edit', params).then((res) => {
          if (res.code == 200) {
            this.$elMessage.success(res.message)
          } else {
            this.$elMessage.error(res.message)
          }
        })
      }
    },
    // 关闭
    handleClose() {
      this.$EventBus.$emit('closePage', '/setting/pricePriority/index')
    },
  },
  components: {
    PublicComponent,
  },
}
</script>

<style scoped lang='scss'>
.isActive {
  background-color: #1993fb;
  color: #fff;
}

.gather {
  margin-left: 0.562535rem;
}

.basic_info {
  width: 100%;

  height: calc(100vh - 1.875117rem);
  background: #f0f2f5;
  box-sizing: border-box;
  padding: 0 0.168761rem 0.168761rem;
  display: flex;
  flex-direction: column;
}

.reset {
  position: relative;
  background-color: #fff;
  height: 100%;

  .btn {
    text-align: center;
  }

  .resetInfo {
    padding: 0.375023rem;

    .title {
      border-bottom: 0.018751rem solid #eee;
      font-size: 0.300019rem;
      color: #1b94fb;
      padding-bottom: 0.281268rem;
    }
  }
}

.el-table {
  margin-left: 0.562535rem;
  margin-top: 0.656291rem;
}

::v-deep .el-table__header th {
  padding: 0;
  height: 0.562535rem;
  text-align: center;
  color: #666666;
}

::v-deep .el-table__body td {
  height: 0.750047rem;
}

.isSave {
  text-align: center;
  margin-top: 1.312582rem;

  .el-button {
    border-radius: 0.093756rem;
  }

  .el-button:first-child {
    background: #1890ff;
    color: #fff;
  }
}

::v-deep .el-table td {
  padding: 0;
}
</style>